// =====================================================================
//
//  This file is part of the Microsoft Dynamics CRM SDK code samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// =====================================================================
#region

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using SL.IdentityModel.Protocols.WSTrust;



#endregion

namespace Microsoft.Xrm.Sdk.Client
{
	/// <summary>
	/// 	Handles retrieving/making use of service metadata information.
	/// </summary>
	internal static class ServiceMetadataUtility
	{
		public static IssuerEndpointDictionary RetrieveLiveIdIssuerEndpoints(IdentityProviderTrustConfiguration identityProviderTrustConfiguration)
		{
			var issuers = new IssuerEndpointDictionary();

			issuers.Add(TokenServiceCredentialType.Username.ToString(),
						new IssuerEndpoint
						{
							CredentialType = TokenServiceCredentialType.Username,
							IssuerAddress = new EndpointAddress(identityProviderTrustConfiguration.Endpoint.AbsoluteUri),
							IssuerBinding = identityProviderTrustConfiguration.Binding,
							TrustVersion = Protocols.WSTrust.WSTrustConstants.Version.WSTrust2005
						});
			var stsBinding = new WSTrustBindingIssuedTokenMixed();

			issuers.Add(TokenServiceCredentialType.SymmetricToken.ToString(),
						new IssuerEndpoint
						{
							CredentialType = TokenServiceCredentialType.SymmetricToken,
							IssuerAddress = new EndpointAddress(identityProviderTrustConfiguration.Endpoint.AbsoluteUri),
							IssuerBinding = stsBinding,
							TrustVersion = Protocols.WSTrust.WSTrustConstants.Version.WSTrust2005
						});
			return issuers;
		}

		public static IssuerEndpointDictionary RetrieveDefaultIssuerEndpoint(AuthenticationProviderType authenticationProviderType, IssuerEndpoint issuer)
		{
			var issuers = new IssuerEndpointDictionary();

			if (issuer != null && issuer.IssuerAddress != null)
			{
				//Default to username
				TokenServiceCredentialType credentialType;

				//Go ahead and add the auth endpoint.  We'll assume username for now, since we have nothing to go on.
				switch (authenticationProviderType)
				{
					case AuthenticationProviderType.LiveId:
						credentialType = TokenServiceCredentialType.Username;
						break;
					case AuthenticationProviderType.Federation:
						credentialType = TokenServiceCredentialType.Kerberos;
						break;
					case AuthenticationProviderType.OnlineFederation:
						credentialType = TokenServiceCredentialType.Username;
						break;
					default:
						credentialType = TokenServiceCredentialType.Kerberos;
						break;
				}

				issuers.Add(credentialType.ToString(),
							new IssuerEndpoint
							{
								CredentialType = credentialType,
								IssuerAddress = issuer.IssuerAddress,
								IssuerBinding = issuer.IssuerBinding
							});
			}
			return issuers;
		}
		public static IssuerEndpoint GetIssuer(Binding binding)
		{
			if (binding == null)
			{
				return null;
			}
			var elements = binding.CreateBindingElements();
			var securityElement = elements.Find<SecurityBindingElement>();


			return new IssuerEndpoint();
		}

		public static CustomBinding SetIssuer(Binding binding, IssuerEndpoint issuerEndpoint)
		{
			throw new NotImplementedException();
		}

		private static bool IsEndpointSupported(ServiceEndpoint endpoint)
		{
			if (endpoint != null)
			{
				//The web endpoints are currently designed for in-browser JScript usage and not by the rich client.
				if (!endpoint.Address.Uri.AbsolutePath.EndsWith(XrmServiceConstants.WebEndpointExtension, StringComparison.OrdinalIgnoreCase))
				{
					return true;
				}
			}
			return false;
		}
	}
	internal static class XrmServiceConstants
	{
		public const String WebEndpointExtension = "web";
	}
}
